package day1;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;

import utility.JdbcUtils;


public class DateTest {
	public static void main(String[] args) throws Exception {
		//add();
		query();
	}
	
	public static void add() throws SQLException {
		Connection connection=null;
		PreparedStatement ps=null;
		ResultSet resultSet=null;
		try {
			connection=JdbcUtils.getConnection();
			String sql="insert into user(name,birthday, money) values (?, ?, ?) ";
			ps=connection.prepareCall(sql);
			ps.setString(1, "dd");
			ps.setDate(2, new java.sql.Date(new Date().getTime()));
			ps.setFloat(3, 111.1f);
			
			int i = ps.executeUpdate();
			System.out.println("i=" + i);
		} finally{
			JdbcUtils.free(resultSet, ps, connection);
		}
	}
	
	public static void query() throws SQLException {
		Connection connection=null;
		PreparedStatement ps=null;
		ResultSet resultSet=null;
		try {
			connection=JdbcUtils.getConnection();
			String sql="select  name,birthday, money from user";
			ps=connection.prepareCall(sql);
			resultSet=ps.executeQuery();
			while(resultSet.next()){
				String nameString=resultSet.getString(1);
				Date birthday=resultSet.getDate(2);
				float	money=resultSet.getFloat(3);
				System.out.println(nameString+" | "+birthday+" | "+money);
				
			}
		} finally{
			JdbcUtils.free(resultSet, ps, connection);
		}
	}
}
